﻿using System;
using System.Web.Services;
using Besture.Common.Definition;
using Besture.Common.Helper;
using Besture.Model;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
using Besture.Common;
using Besture.Bll;

namespace Besture.UI.Admin.Content
{
    public partial class ImgsList : BasePage
    {
        //栏目编号
        public string PartID = StringHelper.Query("partid");

        #region 页面加载

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            //初始化页面
            InitPage();
            BindData();
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData();
        }


        /// <summary>
        /// 初始化页面
        /// </summary>
        public void InitPage()
        {
           
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            if(string.IsNullOrEmpty(PartID))
            {
                PartID = "0";
            }

            string sql =
                string.Format(
                    @"SELECT 
	                    tc.*,t.Name AS PartName 
                    FROM 
	                    TBPartContent tc
                    LEFT JOIN 
	                    TBPart t 
                    ON 
	                    tc.PartID = t.ID 
                    WHERE 
	                    ( tc.partid IN (SELECT ID FROM PartTree({0}))
                    OR 
	                    tc.partid = {0} )
                    ORDER BY
                        tc.id DESC",PartID);
            //grid.DataSource = QueryDac.GetTable(sql);
            //grid.DataBind();
        }


        #endregion

        #region 删除数据

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;
            int id = Convert.ToInt32(e.Keys[0]);
            PartContentDac partContentDac = new PartContentDac();
            partContentDac.Delete(id);
            BindData();
        }

        /// <summary>
        /// 删除选中数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [WebMethod]
        public static string DeleteSelect(string ids,string id)
        {
            string strIds = string.Empty;
            if (string.IsNullOrEmpty(id))
            {
                 strIds= ids.Substring(0, ids.Length - 1);    
            }
            else
            {
                strIds = id;
            }

            if (string.IsNullOrEmpty(strIds)) return "0";
            
            PartContentDac dac = new PartContentDac();
            if (dac.DeleteList(strIds)>0)
            {
                return "1";
            }
            return "0";
        }

        #endregion

        #region 转向添加页面

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="eventArgs"></param>
        protected void btnAdd_Click(object obj, EventArgs eventArgs)
        {
            RedirectUrl("ListImgsSet.aspx?partid=" + PartID);
        }

        #endregion
        
       
    }
}